home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Languages / Masm V6.11 / INCLUDE / MACROS.IN$ / MACROS
Encoding:
Text File  |  1992-09-24  |  6.0 KB  |  213 lines

  1. ; Utility Macros - Version 1.0 - for Microsoft Macro Assembler 6.0
  2. ; (C) Copyright Microsoft Corporation, 1987,1988,1989,1990
  3.  
  4. ;* @ArgCount - Macro function returns the number of arguments in a
  5. ;* VARARG list.
  6. ;*
  7. ;* Params:  arglist - arguments to be counted
  8.  
  9. @ArgCount MACRO arglist:VARARG
  10.     LOCAL count
  11.     count = 0
  12.     FOR arg, <arglist>
  13.         count = count + 1
  14.     ENDM
  15.     EXITM %count
  16. ENDM
  17.  
  18. ;* @ArgI - Macro function returns an argument specified by number
  19. ;* from a VARARG list.
  20. ;*
  21. ;* Shows:   Directives - FOR       LOCAL    EXITM
  22. ;*                       TEXTEQU   =
  23. ;*          Operator   - EQ
  24. ;*
  25. ;* Params:  index - one-based number of the argument to be returned
  26. ;*          arglist - argument list
  27.  
  28. @ArgI MACRO index:REQ, arglist:VARARG
  29.     LOCAL count, retstr
  30.     count = 0
  31.     FOR arg, <arglist>
  32.         count = count + 1
  33.         IF count EQ index
  34.             retstr TEXTEQU <arg>
  35.         ENDIF
  36.     ENDM
  37.     EXITM retstr
  38. ENDM
  39.  
  40. ;* @ArgRev - Macro function returns a reversed order version of a
  41. ;* VARARG list.
  42. ;*
  43. ;* Shows:   Operators           - <>         !        %
  44. ;*          String directive    - SUBSTR
  45. ;*          Predefined function - @SizeStr
  46. ;*
  47. ;* Params:  arglist - arguments to be reversed
  48.  
  49. @ArgRev MACRO arglist
  50.     LOCAL txt, arg
  51.     txt TEXTEQU <>
  52. %   FOR arg, arglist
  53.         txt CATSTR <arg>, <!,>, txt
  54.     ENDM
  55.  
  56.     txt SUBSTR  txt, 1, @SizeStr( %txt ) - 1
  57.     txt CATSTR  <!<>, txt, <!>>
  58.     EXITM txt
  59. ENDM
  60.  
  61. ;* @SaveRegs and @RestoreRegs - Macros to save and restore a list
  62. ;* of macros. @SaveRegs pushes each register in the argument list.
  63. ;* and saves each register name in a text macro. @RestoreRegs uses
  64. ;* the saved register list to pop each register.
  65. ;*
  66. ;* Shows:   Directives - CATSTR    INSTR
  67. ;*
  68. ;*
  69. ;* Params:  For @SaveRegs, the registers to be pushed
  70. ;*          For @RestoreRegs, none
  71.  
  72. pregs   TEXTEQU <>  ; Initialize global text macro to empty
  73.  
  74. @SaveRegs MACRO regs:VARARG
  75.         LOCAL reg
  76. pregs   CATSTR  <!<>, <regs>, <!>>
  77. %       FOR reg, <regs>
  78.             push reg                                ;; Push each register
  79.         ENDM
  80. ENDM
  81.  
  82. @RestoreRegs MACRO
  83.     LOCAL   reg
  84. %   FOR reg, @ArgRev( pregs )                   ;; @ArgRev from MACROS.INC
  85.         pop reg                                 ;; Pop each register
  86.     ENDM
  87. ENDM
  88.  
  89. ;* @PushAll and @PopAll - Macros to push and pop all general purpose
  90. ;* registers. Registers are pushed in the following order:
  91. ;*    AX, CX, DX, BX, SP, BP, SI, DI
  92. ;* They are popped in the reverse order. Uses most efficient method
  93. ;* available at assembly time (not at run time).
  94. ;*
  95. ;* Shows:   Instruction - pusha     popa
  96. ;*          Operator    - AND
  97. ;*
  98. ;* Params:  None
  99.  
  100. @PushAll MACRO
  101.     IF @Cpu AND 00000010y       ;; If assembling on 80186/286/386,
  102.         pusha                   ;;  use efficient PUSHA
  103.     ELSE                        ;; Otherwise push individually
  104.         push ax
  105.         push cx
  106.         push dx
  107.         push bx
  108.         push sp
  109.         push bp
  110.         push si
  111.         push di
  112.     ENDIF
  113. ENDM
  114.  
  115. @PopAll MACRO
  116.     IF @Cpu AND 00000010y       ;; If assembling on 80186/286/386,
  117.         popa                    ;;  use efficient POPA
  118.     ELSE                        ;; Otherwise pop individually
  119.         pop di
  120.         pop si
  121.         pop bp
  122.         pop sp
  123.         pop bx
  124.         pop dx
  125.         pop cx
  126.         pop ax
  127.     ENDIF
  128. ENDM
  129.  
  130. ;* echof - Macro to display assembly-time value of expressions. The
  131. ;* syntax is similar to the C printf function. Useful for debugging
  132. ;* macros. For example, the following line displays the SIZE of an
  133. ;* array:
  134. ;*          echof  <The value of $ is $>, (SIZE array), %(SIZE array)
  135. ;*
  136. ;* Params:  format - Text to be displayed with a $ placeholder for
  137. ;*                   each expression to be evaluated and inserted.
  138. ;*          args -   List of expressions to be evaluated. Text of
  139. ;*                   each value will be inserted into the format.
  140.  
  141. echof   MACRO   format:REQ, args:VARARG
  142.     LOCAL   string, pos, lastpos
  143.  
  144.     ;; Initialize variables
  145.     pos     =   1
  146.     string  TEXTEQU <>
  147.     ;; Loop through, finding $ and building output string
  148.     FOR val, <args>
  149.         ;; If beyond end of format string, exit FOR loop
  150.         IF pos GE @SizeStr( format )
  151.             pos = 0
  152.             EXITM
  153.         ENDIF
  154.         ;; Save last position and find the next $
  155.         lastpos = pos
  156.         pos     = @InStr( pos, format, <$> )
  157.         ;; If $ not found, exit FOR loop
  158.         IF pos EQ 0
  159.             EXITM
  160.         ENDIF
  161.         ;; Append text up the next $
  162.         string  CATSTR string, @SubStr( format, lastpos, pos - lastpos )
  163.         ;; Append matching value and skip past $
  164.         string  CATSTR string, <val>
  165.         pos     = pos + 1
  166.     ENDM
  167.     IF pos
  168.         ;; Attach any trailing characters
  169.         IF pos LE @SizeStr( format )
  170.             string CATSTR string, @SubStr( format, pos )
  171.         ENDIF
  172.         ;; Display the finished string
  173. %       ECHO string
  174.     ELSE
  175.         ECHO echof error: $ count does not match argument count
  176.     ENDIF
  177. ENDM
  178.  
  179. ;* Equates for conditional handling of pointers
  180.  
  181.         IF @DataSize
  182. lesIF   TEXTEQU    <les>
  183. ldsIF   TEXTEQU    <lds>
  184. esIF    TEXTEQU    <es:>
  185.         ELSE
  186. lesIF   TEXTEQU    <mov>
  187. ldsIF   TEXTEQU    <mov>
  188. esIF    TEXTEQU    <>
  189.         ENDIF
  190.  
  191. ;* pushc - Macro that pushes a constant using the most efficient
  192. ;* method for the current processor. The 80286 version pushes
  193. ;* directly. Since this is illegal on the 8086, this version pushes
  194. ;* through AX.
  195. ;*
  196. ;* Shows:   Operator    - OPATTR
  197. ;*          Text macro  - @Cpu
  198.  
  199. IF  @Cpu AND 00000010y
  200.     pushc  MACRO op             ;; 80186 or higher
  201.         push op
  202.     ENDM
  203. ELSE
  204.     pushc  MACRO op             ;; 8088/8086
  205.         IFE (OPATTR (adr)) AND 00000100y
  206.             mov  ax, op         ;; If it's really a constant
  207.             push ax             ;;   push through register
  208.         ELSE
  209.             push op             ;; If not constant, push normal
  210.         ENDIF
  211.     ENDM
  212. ENDIF
  213.